package cheesymock;

import java.lang.reflect.*;

/**
 * Validator that ensures that methods in the delegate are actually
 * overriden methods/properties from the interface we are mocking.
 *
 * @author fredrik
 */
public class StrictMethodOverride extends Validator {

	@Override
	public void creation(Class<?> clazz, Object delegate) {
		if (delegate != null) {
			for(Method m: delegate.getClass().getDeclaredMethods()) {
				if (m.getAnnotation(Check.class) != null || m.getAnnotation(Ignore.class) != null) {
				    break;
				}
				try {
					clazz.getMethod(m.getName(), m.getParameterTypes());
				}
				catch (NoSuchMethodException e1) {
					throw new IllegalArgumentException("Delegate object has declared method " + 
							m.getName() + ", but it doesn't exist in interface " + clazz.getName() + 
							". A refactoring gone wrong, perhaps?");
				}
			}
		}
	}
}